var $ = require("../../utils/Ajax.js");
var WxParse = require('../../utils/wxParse/wxParse.js');
Page({
  data: {
    // 图片路径配置
    PictureUrl: getApp().PictureUrl,

    movies: null,

    // 底部所有目录控制
    showShopPopup: false,
    animationData: {},

    // 商品ID
    Id: null,

    Type: 0,

    // 商品信息
    ShtopDetails: null,

    // 商品价格
    ShopPrice: null,

    // 商品库存
    Stock: null,

    // 评论信息
    CommentCont: null,
    CommentContL: 0,

    // 滑动购物车按钮时,禁止页面滚动
    Scroll: true,

    // 商品属性下标
    AttrIndex: 0,

    // 加入购物车的数量
    ShopNumber: 1,

    // 加入购物车商品的属性ID
    AttrId: null,

    // 购物车商品数量
    Quantity: 0,

    VipMoney: '查看会员价格',

    // 直接购买
    SubmitGoods: null,
  },

  onLoad: function (options) {
    var that = this;
    wx.setNavigationBarTitle({
      title: "商品详情"
    }) 
    that.setData({
      Id: options.Id,
      Type: options.Type
    })
    $.Ajax("api/goods","GET",{
      id: options.Id,
      isActivity: that.data.Type
    },function(res){
      if(res.s){
        console.log(res)
        WxParse.wxParse('article', 'html', res.d.goods.details, that, 0);
        that.setData({
          CommentCont: res.d.goods.comment,
          CommentContL: res.d.goods.comment.length,
          ShtopDetails: res.d.goods,
          ShopPrice: res.d.goods.attr[0].shopPrice,
          Stock: res.d.goods.attr[0].stock,
          AttrId: res.d.goods.attr[0].id,
        })
      }else{
        wx.showToast({
          title: res.m,
          icon: 'none',
          duration: 1500
        })
      }
    })
  },

  // 分享
  onShareAppMessage: function () {
    var that = this;
    var Title = that.data.ShtopDetails.title;
    var Url = that.data.PictureUrl + that.data.ShtopDetails.album[0].url;
    var shareObj = {
      title: Title,
      path: '/pages/ShopDetails/ShopDetails?id=' + this.data.Id + '&isActivity=0',
      imageUrl: Url,
      success: function (res) { }
    }
    return shareObj;
  },


  onShow: function () {
    var that = this;
    that.setData({
      SubmitGoods: null,
      ShopNumber: 1
    })
    $.Ajax("api/cart", "GET", {}, function (res) {
      if (res.s) {
        that.setData({
          Quantity: res.d.cartNum
        })
      } else {
        wx.showToast({
          title: res.m,
          icon: 'none',
          duration: 2000
        })
      }
    })
  },

  // 查看会员价格
  LookVip: function(){
    var level = wx.getStorageSync('UserInfor').level;
    if (level >= 0 && level <= 2){
      this.setData({
        VipMoney: this.data.ShtopDetails.attr[0].memberPrice1
      })
    } else if (level >= 3 && level <= 10){
      this.setData({
        VipMoney: this.data.ShtopDetails.attr[0].memberPrice2
      })
    } else if (level >= 11 && level <= 19) {
      this.setData({
        VipMoney: this.data.ShtopDetails.attr[0].memberPrice3
      })
    } else if (level >= 20 && level <= 26) {
      this.setData({
        VipMoney: this.data.ShtopDetails.attr[0].memberPrice4
      })
    } else if (level >= 27 && level <= 29) {
      this.setData({
        VipMoney: this.data.ShtopDetails.attr[0].memberPrice5
      })
    }else{
      this.setData({
        VipMoney: this.data.ShtopDetails.attr[0].memberPrice6
      })
    }
  },

  // 点击收藏
  Collection: function(e){
    var that = this;
    var Token = wx.getStorageSync('Token');
    var UserInfor = wx.getStorageSync('UserInfor');
    if (Token == '' || Token == null) {
      wx.removeStorageSync('Token');
      wx.removeStorageSync('UserInfor');
      wx.showToast({
        title: "请先登录",
        icon: 'none',
        duration: 1500
      })
      setTimeout(function () {
        wx.redirectTo({
          url: '../My/My',
        })
      }, 2000)
    }else{
      var Id = e.currentTarget.dataset.id;
      $.Ajax("api/addCollection", "POST", {
        id: Id
      }, function (res) {
        if (res.s) {
          wx.showToast({
            title: res.m,
            icon: 'success',
            duration: 3000,
            mask: true
          })
          var ShtopDetails = that.data.ShtopDetails;
          if (ShtopDetails.isCollection == 0){
            ShtopDetails.isCollection = 1;
            that.setData({
              ShtopDetails: ShtopDetails
            })
          }
        } else {
          wx.showToast({
            title: res.m,
            icon: 'none',
            duration: 3000,
            mask: true
          })
        }
      })
    }
  },

  // 点击打开购物车
  Cart: function () {
    var Token = wx.getStorageSync('Token');
    var UserInfor = wx.getStorageSync('UserInfor');
    if (Token == '' || Token == null) {
      wx.removeStorageSync('Token');
      wx.removeStorageSync('UserInfor');
      wx.showToast({
        title: "请先登录",
        icon: 'none',
        duration: 1500
      })
      setTimeout(function () {
        wx.switchTab({
          url: '../My/My',
        })
      }, 1500)
    } else {
      wx.navigateTo({
        url: '../Cart/Cart',
      })
    }
  },


  // 手指拖动购物车按钮
  setTouchMove: function (e) {
    //此处clientY与clientX为拖动悬浮窗超过设定的大小会返回默认显示位置
    if (e.touches[0].clientX > 30 && e.touches[0].clientY > 80 && e.touches[0].clientX < 370 && e.touches[0].clientY < 600) {
      this.setData({
        Scroll: false,
        left: e.touches[0].clientX - 20,
        top: e.touches[0].clientY - 20
      })
    }
  },

  // 手指离开购物车按钮
  setTouchEnd: function () {
    this.setData({
      Scroll: true
    })
  },

  // 点击加入购物车
  JoinCart: function(){
    var animation = wx.createAnimation({
      duration: 400,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(936).step()
    this.setData({
      animationData: animation.export(),
      showShopPopup: true
    })
    setTimeout(() => {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export()
      })
    }, 200)
  },

  // 点击关闭购物车
  CartBgClose: function(e){
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(936).step()
    this.setData({
      animationData: animation.export(),
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export(),
        showShopPopup: false
      })
    }.bind(this), 200)
    var that = this;
    var Token = wx.getStorageSync('Token');
    var UserInfor = wx.getStorageSync('UserInfor');
    var Status = e.currentTarget.dataset.status;
    var SubmitGoods = e.currentTarget.dataset.submitgoods
    if (Token == '' || Token == null) {
      wx.removeStorageSync('Token');
      wx.removeStorageSync('UserInfor');
      wx.showToast({
        title: "请先登录",
        icon: 'none',
        duration: 1500
      })
      setTimeout(function () {
        wx.switchTab({
          url: '../My/My',
        })
      }, 1500)
    } else {
      if (Status == 1 && SubmitGoods == 1) {
        $.Ajax("api/buy", "POST", {
          id: that.data.Id,
          aid: that.data.AttrId
        }, function (res) {
          if (res.s) {
            if (res.d.address == null) {
              wx.showToast({
                title: '收货地址不能为空',
                icon: 'none',
                duration: 1500,
                mask: true
              })
              setTimeout(function () {
                wx.navigateTo({
                  url: '../ShippingAddress/ShippingAddress',
                })
              }, 1500)
            } else {
              var BuyInfor = JSON.stringify(res.d);
              wx.navigateTo({
                url: '../SubmitBuy/SubmitBuy?BuyInfor=' + BuyInfor + '&Type=' + that.data.Type,
              })
            }
          } else {
            wx.showToast({
              title: res.m,
              icon: 'none',
              duration: 1500,
              mask: true
            })
          }
        })
      } else if (Status == 1){
        $.Ajax("api/addCart", "POST", {
          id: Number(this.data.Id),
          aid: this.data.AttrId,
          num: this.data.ShopNumber
        }, function (res) {
          that.setData({
            Quantity: that.data.Quantity + that.data.ShopNumber
          })
          wx.showToast({
            title: res.m,
            icon: 'none',
            duration: 1500,
            mask: true
          })
        })
      }
    }
  },

  // 选择商品属性
  AttrIndex: function(e){
    var AttrIndex = e.currentTarget.dataset.index_;
    var AttrId = e.currentTarget.dataset.id;
    this.setData({
      AttrIndex: AttrIndex,
      AttrId: AttrId
    })
  },

  // 点击减少数量
  ShopJian: function(){
    var ShopNumber = this.data.ShopNumber;
    if (ShopNumber == 1){
      wx.showToast({
        title: "不能再减了",
        icon: 'none',
        duration: 3000,
        mask: true
      })
    }else{
      --ShopNumber;
      this.setData({
        ShopNumber: ShopNumber
      })
    }
  },

  // 点击增加数量
  ShopJia: function(){
    var ShopNumber = this.data.ShopNumber;
    ++ShopNumber;
    this.setData({
      ShopNumber: ShopNumber
    })
  },

  // 立即购买
  StoreTea: function (e) {
    var that = this;
    var Attr = that.data.ShtopDetails.attr;
    if (Attr.length > 1 && !(that.data.showShopPopup)){
      that.JoinCart();
      that.setData({
        SubmitGoods: 1
      })
    }else{
      $.Ajax("api/buy", "POST", {
        id: that.data.Id,
        aid: that.data.AttrId
      }, function (res) {
        if(res.s){
          if (res.d.address == null){
            wx.showToast({
              title: '收货地址不能为空',
              icon: 'none',
              duration: 1500,
              mask: true
            })
            setTimeout(function(){
              wx.navigateTo({
                url: '../ShippingAddress/ShippingAddress',
              })
            },1500)
          }else{
            var BuyInfor = JSON.stringify(res.d);
            wx.navigateTo({
              url: '../SubmitBuy/SubmitBuy?BuyInfor=' + BuyInfor + '&Type=' + that.data.Type,
            })
          }
        }else{
          wx.showToast({
            title: res.m,
            icon: 'none',
            duration: 1500,
            mask: true
          })
        }
      })
    }
  }
})